module mirevent;

import std.datetime;

import mirdefine, mirshare;
import common.gameutil;
import share.protocol, share.container;

import allegro5.allegro;


const ZOMBIDIGUPDUSTBASE = 420;
const STONEFRAGMENTBASE = 64;
const HOLYCURTAINBASE = 1390;
const FIREBURNBASE = 1630;
const SCULPTUREFRAGMENT = 1349;
const HEARTPALPMARK = 420;


public struct Event
{
	public int X, Y, Dir;
	public int px, py;
	public int EventType;
	public int EventParam;
	public int  ServerId;
	public ALLEGRO_BITMAP* bmp;
	public bool BoBlend;
	public TickDuration calcframetime;
	public uint curframe;
	public int light;

	public this(int svid, int ax, int ay, int evtype)
	{
		ServerId = svid;
		X = ax;
		Y = ay;
		EventType = evtype;
		EventParam = 0;
		BoBlend = false;
		calcframetime = Clock.currSystemTick();
		curframe = 0;
		light = 0;
	}

	public void renderAt(int ax, int ay)
	{
		if (bmp !is null)
		{
			if (BoBlend)
				drawBlend(bmp, ax + px, ay + py, 1);
			else
				al_draw_bitmap(bmp, ax + px, ay + py, 0);
		}
	}
	
	public void update()
	{
		TickDuration curtime = Clock.currSystemTick();
		if ((curtime - calcframetime).msecs > 20)
		{
			calcframetime = curtime;
			curframe++;
		}
		
		bmp = null;
		/+final switch (EventType)
		{
			case ET_DIGOUTZOMBI:
				{
					bmp = ImageMan[_IMAGE_MONSTER6].GetCachedImage(ZOMBIDIGUPDUSTBASE + Dir, px, py);
					break;
				}
				
			case ET_PILESTONES:
				{
					if (EventParam <= 0) EventParam = 1;
					if (EventParam > 5) EventParam = 5;
					bmp = ImageMan[_IMAGE_EFFECT].GetCachedImage(STONEFRAGMENTBASE + (EventParam - 1), px, py);
					break;
				}
		
			case ET_HOLYCURTAIN:
				{
					bmp = ImageMan[_IMAGE_MAGIC].GetCachedImage(HOLYCURTAINBASE + (curframe % 10), px, py);
					BoBlend = true;
					light = 1;
					break;
				}
	
			case ET_FIRE:
				{
					// TODO:
					/*if (BoViewEffect)
					{
						bmp = ImageMan[_IMAGE_MAGIC].GetCachedImage(FIREBURNBASE + ((curframe div 2) mod 6), px, py);
						BoBlend = true;
						light = 1;
					}*/
					break;
				}

			case ET_SCULPEICE:
				{
					bmp = ImageMan[_IMAGE_MONSTER7].GetCachedImage(SCULPTUREFRAGMENT, px, py);
					break;
				}

			case ET_HEARTPALP: //Àû¿ù¸¶ÀÇ ÃË¼ö °ø°Ý
				{
					bmp = ImageMan[_IMAGE_MONSTER14].GetCachedImage(HEARTPALPMARK, px, py);
					break;
				}
		}+/
	}
}
alias Event* PEvent;

final class EventManager
{
	public Vector!PEvent EventList;


	public this()
	{
	}
	
	public ~this()
	{
		clearEvents();
	}
	
	public void clearEvents()
	{
		for (int i = 0; i < EventList.count; i++)
		{
			PEvent levn = EventList[i];
            destroy(levn);
		}
		EventList.clear();
	}
	
	public bool addEvent(PEvent evn)
	{
		for (int i = 0; i < EventList.count; i++)
		{
			PEvent levn = EventList[i];
			if ((levn is evn) || (levn.ServerId == evn.ServerId))
			{
                destroy(levn);
				EventList.deleteAt(i);
				return false;
			}
		}
		EventList.add(evn);
		return true;
	}
	
	public bool deleteEvent(PEvent evn)
	{
		for (int i = 0; i < EventList.count; i++)
		{
			PEvent levn = EventList[i];
			if (levn is evn)
			{
                destroy(levn);
				EventList.deleteAt(i);
				return true;
			}
		}
		return false;
	}
	
	public bool deleteEventById(int svid)
	{
		for (int i = 0; i < EventList.count; i++)
		{
			PEvent levn = EventList[i];
			if (levn.ServerId == svid)
			{
                destroy(levn);
				EventList.deleteAt(i);
				return true;
			}
		}
		return false;
	}
	
	public PEvent findEvent(int ax, int ay, int etype)
	{
		for (int i = 0; i < EventList.count; i++)
		{
			PEvent levn = EventList[i];
			if ((levn.X == ax) && (levn.Y == ay) && (levn.EventType == etype))
				return EventList[i];
		}
		
		return null;
	}
	
	public void update()
	{
		for (int i = 0; i < EventList.count; i++)
			EventList[i].update();
	}
}